<?php

class NewsController extends Controller
{
	public function actionDetail($id)
	{
      $criteria = new CDbCriteria();
      $criteria->addCondition('active = 1');
      $criteria->order = 'hot DESC,modify_date DESC,create_date DESC';
      $criteria->offset = 0;
      $criteria->limit = 23;
      $top_news = News::model()->findAll($criteria);
      
      $model = News::model()->findByPk($id);
      //CVarDumper::dump($model,10,true);die();
      if($model == null)
         $this->redirect(Yii::app()->request->hostInfo);
      
      //Tăng view
      setcookie('allow', '1', time()+3600);
      if(isset($_COOKIE['allow']) && $_COOKIE['allow']=='1'){
         $list_id_viewed = isset($_COOKIE['list_id_viewed']) ? json_decode($_COOKIE['list_id_viewed'],true) : array();
         
         if(!in_array($id,$list_id_viewed)){
            $model->view = $model->view + 1;
            $model->save();
            $list_id_viewed[] = $id;
            setcookie('list_id_viewed',json_encode($list_id_viewed),time()+3600);
         }
         
      }
      
      //Lấy tin liên quan
      $criteria = new CDbCriteria();
      $criteria->addCondition('cat_id =' . $model->cat_id);
      $criteria->addCondition('id <' . $model->id);
      $criteria->order = 'id DESC';
      $criteria->limit = 8;
      $related_news = News::model()->findAll($criteria);
      
      //CVarDumper::dump($related_news,10,true);die();
	   $url_curent = Yii::app()->request->hostInfo.News::model()->getDetailLink($model->id,$model->title);  
      $description = ($model->sapo != '') ?  $model->sapo : Str::cutString(Str::removeHTML($model->content),150);
      $this->pageTitle = $model->title;
      
      //Meta tag for facebook
      Yii::app()->clientScript->registerMetaTag($model->title, 'og:title');
      Yii::app()->clientScript->registerMetaTag('article', 'og:type');
      Yii::app()->clientScript->registerMetaTag(Yii::app()->request->hostInfo.News::model()->getImageThumbnail($model->image,600), 'og:image');
      Yii::app()->clientScript->registerMetaTag($url_curent, 'og:url');
      Yii::app()->clientScript->registerMetaTag($description, 'og:description');
      
      //Meta tag for google
      Yii::app()->clientScript->registerMetaTag($description, 'description');
      Yii::app()->clientScript->registerMetaTag('toancanhbongda.com', 'author');
      $this->render('detail',array(
         'model' => $model,
         'related_news' => $related_news,
         'url_curent' => $url_curent,
         'top_news' => $top_news,
      ));
	}

	public function actionIndex()
	{
		$cat_id = Yii::app()->request->getParam('cat_id',0);
      $page = Yii::app()->request->getParam('page',1);
      $limit = 11;
      $offset = $page*$limit-$limit;
      
      $criteria = new CDbCriteria();
      $criteria->addCondition('active = 1');
      $cate = null;
      if($cat_id > 0){
         $cate = Category::model()->findByPk($cat_id);
         if(!is_null($cate)){
            $this->pageTitle = $cate->name . ' | Tổng hợp tin tức 24/7';
         }
         $list_cat_child = array($cat_id);
         $list_object_cat = Category::model()->findAllByAttributes(array('parent_id' => $cat_id));
         if(count($list_object_cat) > 0){
            foreach($list_object_cat as $cat_child){
               $list_cat_child[] = $cat_child->id;
            }
         }
         $criteria->addInCondition('cat_id',$list_cat_child);
      }
      $criteria->order = 'id DESC';
      $criteria->offset = $offset;
      $criteria->limit = $limit;
      
      $total = News::model()->count($criteria);
      $list_news = News::model()->findAll($criteria);
      
      $pagination = new Zebra_Pagination();
      $pagination->records($total);
      $pagination->records_per_page($limit);
      $pagination->selectable_pages(5);
      $pagination->labels('Trước','Sau');   
      $this->render('index',array(
         'list_news' => $list_news,
         'pagination' => $pagination,
         'cate' => $cate,
      ));
	}
   
   
   /**
    * 
    * Đếm like, comment và cập nhật vào database
   */
   public function actionSocialPlugin(){
      $id = Yii::app()->request->getParam('id');
      if($id != null){
         $model = News::model()->findByPk($id);   
         $url = Yii::app()->request->hostInfo.News::model()->getDetailLink($model->id,$model->title);
         $json = json_decode(file_get_contents('https://graph.facebook.com/?ids=' . $url));
         $comments = isset($json->$url->comments) ? $json->$url->comments : 0;
         $like = isset($json->$url->shares) ? $json->$url->shares : 0;
         $model->count_like = $like;
         $model->comment = $comments;
         $model->save();
      }
      
   }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}